package com.apache.info.dao;

import com.apache.cache.util.Validator;
import com.apache.database.constant.SystemTools;
import com.apache.database.db.DaoImpl;
import com.apache.database.model.MethodParam;
import com.apache.info.entity.PubMetadata;
import com.apache.info.entity.PubSortInfo;
import com.apache.info.util.MessageUtil;
import org.apache.log4j.Logger;

import java.util.*;

public class PubMetadataDaoImpl extends DaoImpl {
	protected Logger log = Logger.getLogger(getClass());

	public String getSql(int type) {
		String sql = "";
		switch (type) {
		case 1:
			sql = "select count(*) from PubMetadata where 1=1 ";
			break;
		case 2:
			sql = " from PubMetadata where 1=1 ";
			break;
		default:
			sql = "select count(*) from PubMetadata where 1=1 ";
		}
		return sql;
	}

	public void initSystemCache() {
		log.info("初始化缓存数据——动态数据表 start...");
		long starTime1 = new Date().getTime();

		MethodParam param = new MethodParam("ByProperty", "", " from PubSortInfo", PubSortInfo.class.getName());
		List<PubSortInfo> list = this.select(param);
		int ct = list.size();
		log.info("[PubSortInfo] query result size[" + ct + "] time[" + (new Date().getTime() - starTime1) + "ms]");
		if (!Validator.isEmpty(list)) {
			long starTime2 = new Date().getTime();
			param = new MethodParam("ByInitMetadata", "", " from PubMetadata", PubMetadata.class.getName());
			//param.setParams("reserved2", "T");
			List<Map> lists = this.select(param);
			int mcount = lists.size();
			log.info("table [PubMetadata] query result size[" + mcount + "] time[" + (new Date().getTime() - starTime2)
					+ "ms]");
			for (int i = 0; i < ct; i++) {
				PubSortInfo info = list.get(i);
				long starTime3 = new Date().getTime();
				String key = MessageUtil.getTableCacheKey(info.getSortCode(), info.getSysName(), "_tableAttr");
				String key2 = MessageUtil.getTableCacheKey(info.getSortCode(), info.getSysName(), "_tableMaps");
				if (mcount > 0) {
					Map<String, String> cloumn = new HashMap<String, String>();
					Iterator<Map> iter = lists.iterator();
					StringBuilder attrs = new StringBuilder();
					String primaryKey = "";
					String primaryKeys = "";
					boolean flag = false;
					while (iter.hasNext()) {
						Map<String, String> pm = iter.next();
						if (info.getSortId().equals(pm.get("02"))) {
							if ("F".equalsIgnoreCase(pm.get("06")))
								continue;
							attrs.append("," + pm.get("05"));
							cloumn.put(pm.get("05"), pm.get("07"));
							if ("1".equals(pm.get("03"))) {
								primaryKey = pm.get("04");
							} else if ("4".equals(pm.get("03"))) {//联合主键
								primaryKeys += "," + pm.get("04");
							}
							iter.remove();
							flag = true;
						}
						if (flag && !info.getSortId().equals(pm.get("02"))) {
							break;
						}
					}
					if (attrs.length() == 0) {
						log.error("动态数据表[" + info.getSortCode() + "]的字段在metadata定义的不能为空！！初始化缓存失败！");
						continue;
					} else {
						if (Validator.isNotNull(primaryKeys)) {
							primaryKeys = primaryKeys.substring(1);
						}
						primaryKey = Validator.getDefaultStr(primaryKey, primaryKeys);
						String cacheContent = "PubMetadata;" + attrs.substring(1).toString() + ";" + primaryKey + ";"
								+ info.getInfoTableName();
						log.info("数据——动态数据表[" + info.getSortCode() + "][数据项条数" + (attrs.toString()) + "]");
						SystemTools.getInstance().getCache("").createCacheObject(key, cacheContent);
						SystemTools.getInstance().getCache("").createCacheObject(key2, cloumn);
					}
				}
				log.info("初始化缓存数据——动态数据表[" + ct + "/" + (i + 1) + "][" + info.getSortCode() + "]["
						+ (new Date().getTime() - starTime3) + "ms]");
			}
		}
	}
}
